import java.util.*;

/**
 * @author LKQ
 * @date 2022/3/17 9:04
 * @description
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        String[] ba = {"hit"};
        solution.mostCommonWord("Bob hit a ball, the hit BALL flew far after it was hit.", ba);
    }
    public String mostCommonWord(String s, String[] banned) {
        int n = s.length();
        Set<String> set = new HashSet<>();
        for (String b : banned) set.add(b);
        // 存储单词出现次数
        Map<String, Integer> map = new HashMap<>();
        char[] S = s.toCharArray();
        String ans = null;
        for (int i = 0; i < n; ) {
            if (!Character.isLetter(S[i]) && ++i >= 0) {
                continue;
            }
            int j = i;
            while (j < n && Character.isLetter(S[j])) {
                j++;
            }
            String sub = s.substring(i, j).toLowerCase();
            i = j + 1;
            if (set.contains(sub)) {
                continue;
            }
            map.put(sub, map.getOrDefault(sub, 0) + 1);
            if (ans == null || map.get(sub) > map.get(ans)) {
                ans = sub;
            }

        }
        return ans;
    }
}
